package wave.common.pojo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import wave.common.exception.WaveBusinessException;
import wave.common.pojo.enums.RStatusEnum;

/**
 * @author 夕下风波
 * @date 2024-12-03 21:55
 * @description 统一返回实体
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    //状态码
    private Integer code;
    //返回信息
    private String message;
    //响应数据
    private T data;
    //响应数据
    private boolean success;

    public Result(RStatusEnum statusEnum) {
        this.code = statusEnum.getCode();
        this.message = statusEnum.getText();
    }

    public static <T> Result<T> status(boolean status) {
        if (status) return Result.data(null);
        return Result.fail(RStatusEnum.FAIL);
    }

    public static <T> Result<T> status(boolean status, RStatusEnum statusEnum) {
        if (status) return Result.data(null, statusEnum);
        return Result.fail(RStatusEnum.FAIL);
    }

    public static <T> Result<T> data(T data) {
        return new Result<>(RStatusEnum.SUCCESS.getCode(), RStatusEnum.SUCCESS.getText(), data, true);
    }

    public static <T> Result<T> data(T data, RStatusEnum statusEnum) {
        return new Result<>(statusEnum.getCode(), statusEnum.getText(), data, true);
    }


    public static <T> Result<T> fail(RStatusEnum statusEnum) {
        return new Result<>(statusEnum.getCode(), statusEnum.getText(), null, false);
    }

    public static <T> Result<T> fail(String message) {
        return new Result<>(RStatusEnum.FAIL.getCode(), message, null, false);
    }

    public static <T> Result<T> exception(WaveBusinessException exception) {
        return new Result<>(RStatusEnum.FAIL.getCode(), exception.getMessage(), null, false);
    }

}
